<template>
  <!-- 图件模板上传 -->
  <el-dialog v-model="open" title="图件模板上传" width="40%" @open="openTemplate" @close="cancelTemplate">
    <el-form :model="templateForm" label-width="100px" :rules="templateRules" ref="templateRef">
      <el-form-item label="图件类型" prop="type">
        <el-radio-group v-model="templateForm.type">
          <el-radio v-for="item in templateType" :key="item.value" :label="item.value">{{ item.name }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="图件上传" prop="ultId">
        <file-upload v-model="templateForm.fileList" :fileType="['ult']" :limit="1" @uploadFile="uploadSuccess"
          @delFile="delFile"></file-upload>
      </el-form-item>
      <el-form-item label="图件名称" prop="name">
        <el-input v-model="templateForm.name"></el-input>
      </el-form-item>
      <el-form-item label="图件描述" prop="description">
        <el-input type="textarea" v-model="templateForm.description" :autosize="{ minRows: 4, maxRows: 8 }" resize="none"
          maxlength="100" show-word-limit></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="saveTemplate">保存</el-button>
      <el-button plain @click="cancelTemplate">取消</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import { templateType } from '@/views/quickMapping/common/const'
import { uploadSave, getById, uploadUpdate, checkName } from '@/api/quickMapping/dataManage/mapTemplate'

const props = defineProps({
  open: Boolean,
  templateId: String
})
const emits = defineEmits(['update:open', 'update:templateId', 'getList'])
const { proxy } = getCurrentInstance()
const initForm = () => {
  return {
    type: '0',
    fileList: []
  }
}
const templateForm = ref(initForm())
const checkNameRepeat = (rule, value, cb) => {
  const { name, type, id } = templateForm.value
  checkName({ name, type, id }).then(res => {
    if (res.data) {
      cb()
    } else {
      cb(new Error('图件名称重复！'))
    }
  })
}
const templateRules = {
  type: [{ required: true, message: '请选择图件类型！', trigger: 'change' }],
  ultId: [{ required: true, message: '请上传图件！', trigger: 'change' }],
  name: [
    { required: true, message: '请填写图件名称！', trigger: 'blur' },
    { validator: checkNameRepeat, trigger: 'blur' }
  ]
}
const uploadSuccess = (files) => {
  templateForm.value.ultId = files.map(item => item.attachId).join(',')
  proxy.$refs['templateRef'].clearValidate()
}
const delFile = files => {
  templateForm.value.ultId = files.map(item => item.attachId).join(',')
}
// 打开
const openTemplate = () => {
  if (props.templateId) {
    getById(props.templateId).then(res => {
      templateForm.value = res.data
      const { ultName, ultId } = res.data
      templateForm.value.fileList = [{ name: ultName, attachId: ultId }]
    })
  }
}
// 模板上传-保存
const saveTemplate = () => {
  proxy.$refs.templateRef.validate(valid => {
    if (valid) {
      if (templateForm.value.id) {
        uploadUpdate(templateForm.value).then(res => {
          if (res.code === 200) {
            proxy.$modal.msgSuccess('修改成功！')
            cancelTemplate()
            emits('getList')
          }
        })
      } else {
        uploadSave(templateForm.value).then(res => {
          if (res.code === 200) {
            proxy.$modal.msgSuccess('保存成功！')
            cancelTemplate()
            emits('getList')
          }
        })
      }
    }
  })
}
// 模板上传-取消
const cancelTemplate = () => {
  emits('update:open', false)
  emits('update:templateId', '')
  proxy.resetAllForm(templateForm.value, initForm())
}
</script>